package sk.ownage.p1.featurestore;

import java.io.File;
import java.util.Vector;

import android.util.Log;

import com.google.android.gms.maps.model.LatLng;

public class TrackGeometry {
	Vector<TrackQuadTreeRoot> trackQuadTreeRoots=new Vector<TrackQuadTreeRoot>();
	Vector<Vector<WaypointInfo>> trackWaypointsList=new Vector<Vector<WaypointInfo>>();
	Vector<TrackGeometryThread> threads=new Vector<TrackGeometry.TrackGeometryThread>();
	final String TAG="trackGeometry";
	private String trackRootPath="";
	int trackId;
	TrackReader tr;
	String path;
	
	public TrackGeometry(int trackId){
		this(trackId,"");
	}
	
	public TrackGeometry(int trackId, String trackRootPath){
		this.trackRootPath=trackRootPath;
		this.trackId=trackId;
			int folderId = this.trackId / TrackQuadTreeRoot.DIRECTORY_MAX;
			int fileId = this.trackId % TrackQuadTreeRoot.DIRECTORY_MAX;
			String folderPath = (((folderId / 100) > 0) ? "" : "0")
					+ (((folderId / 10) > 0) ? "" : "0") + folderId;
			String filePath = (((fileId / 100) > 0) ? "" : "0")
					+ (((fileId / 10) > 0) ? "" : "0") + fileId;
			path = ((!this.trackRootPath.equals(""))?this.trackRootPath:TrackQuadTreeRoot.rootPath) + "/" + folderPath + "/" + filePath + "/" ;
			Log.d(TAG,">>prve>>"+this.trackRootPath+"/" + folderPath + "/" + filePath + "/");
			Log.d(TAG,">>druhe>>"+TrackQuadTreeRoot.rootPath+"/" + folderPath + "/" + filePath + "/");
			File geometryFolder=new File(path);
			if(!geometryFolder.exists())
				geometryFolder.mkdirs();
			for(int i=0;i<5;i++){
				trackQuadTreeRoots.add(new TrackQuadTreeRoot(trackId,path, i));
				threads.add(new TrackGeometryThread(i));
				trackWaypointsList.add(new Vector<WaypointInfo>());
			}
			if (!new File(path+"0/size.bin").exists()) {
				String temp=path.substring(0, path.lastIndexOf("/geometry"));
				tr = new TrackReader(trackId,(!this.trackRootPath.equals(""))?temp+ "/tracks":"");
				boolean new_segment = false;
				try {
					tr.readName();
					tr.readActivity();
					tr.readTimestamp();
					tr.readDescription();
					while (tr.hasTrack()) {
						while (tr.hasSegment()) {
							new_segment = true;
							while (tr.hasWaypoint()) {
								double latitude1 = tr.readWaypointLatitude(), longitude1 = tr
										.readWaypointLongitude();
								tr.readWaypointAltitude();
								long time=tr.readWaypointTimestamp();
								LatLng waypoint = new LatLng(latitude1, longitude1);
								this.addWaypoint(waypoint,time, new_segment);
								new_segment = false;
							}
						}
					}
				} catch (Exception e) {
					Log.d(TAG, e.toString());
				}
			}
	}
	
	public void addWaypoint(LatLng position,long timestamp,boolean newSegment){
		for(int i=0;i<5;i++){
			trackWaypointsList.get(i).add(new WaypointInfo(position, timestamp, newSegment));
			if(!threads.get(i).isAlive()){
				threads.set(i, new TrackGeometryThread(i));
				threads.get(i).start();
			}
		}
	}
	
	private class TrackGeometryThread extends Thread{
		int threadNum=0;
		
		public TrackGeometryThread(int _threadNumber){
			threadNum=_threadNumber;
		}
		@Override
		public void run() {
			while (trackWaypointsList.get(threadNum).size()>0) {
				trackQuadTreeRoots.get(threadNum).addWaypointToTrackGeometry(trackWaypointsList.get(threadNum).get(0));
				trackWaypointsList.get(threadNum).remove(0);
			}
		}
		
	}

}
